package com.hkw.servlet;

import com.hkw.model.ClassRoom;
import com.hkw.model.Student;
import com.hkw.service.IClassRoomService;
import com.hkw.service.IStudentService;
import com.hkw.service.impl.ClassRoomServiceImpl;
import com.hkw.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 处理跟新请求的servlet，返回要修改的那条学生信息
 */
@WebServlet(name = "updateStudentServlet",urlPatterns = "/updateStudent")
public class UpdateStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取要删除学生的id
        String id = request.getParameter("id");
        //实例化接口服务
        IStudentService studentService = new StudentServiceImpl();
        //获取要删除学生的信息，并封装成student对象
        Student student = studentService.findStudentById(Integer.parseInt(id));
        IClassRoomService classRoomService = new ClassRoomServiceImpl();
        List<ClassRoom> allId = classRoomService.findAllId();
        //如果不为空，则跳转显示
        if(student != null){
            System.out.println(student);
            request.setAttribute("student",student);
            request.setAttribute("ids",allId);
            request.getRequestDispatcher("update.jsp").forward(request,response);
        }else{
            //为空，则往request里赋值，再跳转
            System.out.println("查找失败");
            response.sendRedirect("studentlist");
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
